package com.iap.common.view.control.code
{
	import com.iap.common.model.Filler;
	import com.iap.common.model.Sample;
	import com.iap.common.model.SampleFiller;
	import com.iap.common.model.VectorImage;
	import com.iap.common.view.control.SampleDisplay;
	import com.iap.common.view.control.VectorImageFillerDisplay;
	
	import org.osflash.signals.Signal;
	
	import spark.components.Group;
	
	public class SampleDisplayCode extends Group
	{
		private static const MIN_WIDTH:uint = 100;
		private static const DEFAULT_WIDTH:uint = 500;
		protected var view:SampleDisplay;
		
		protected var model:Sample;
		protected var vectorModels:Array;
		public var deleteSignal:Signal = new Signal(String);
		public var _sampleFillers:Array;
		public function set sampleFillers(value:Array):void
		{
			_sampleFillers = value;
		}
		public function get sampleFillers():Array
		{
			return _sampleFillers;
		}
		
		public function SampleDisplayCode()
		{
			super();
		}

		public function setSource(model:Sample):void
		{
			this.model = model;
			this.vectorModels = model.vectorModel.vectorImages;
			if(model.sampleFillers != null)
			{
				this.sampleFillers = model.sampleFillers;
			}
			addImages();
		}
		
		protected function init(view:SampleDisplay):void
		{
			this.view = view;
			addImages();
		}
		
		private function addImages():void
		{
			if(view == null) return;
			if(model == null) return;
			clearImages();
			for each(var item:VectorImage in vectorModels)
			{
				addImage(item, getSampleFillerByLayer(item.layer));
			}
		}
		
		private function getSampleFillerByLayer(layer:uint):SampleFiller
		{
			var result:SampleFiller = null;
			for each(var item:SampleFiller in sampleFillers)
			{
				if(item.layer == layer)
					result = item;
			}
			return result;
		}
		
		private function clearImages():void
		{
			view.main.removeAllElements();
		}
		
		private function addImage(item:VectorImage, sampleFiller:SampleFiller = null):void
		{
			var control:VectorImageFillerDisplay = new VectorImageFillerDisplay();
			control.width = view.width > MIN_WIDTH ? view.width : DEFAULT_WIDTH;
			control.height = view.height > MIN_WIDTH ? view.height : DEFAULT_WIDTH;
			var filler:Filler = sampleFiller ? sampleFiller.filler : null;
			control.setSource(item, filler);
			view.main.addElement(control);
		}
		
		public function setSampleFiller(sampleFiller:SampleFiller):void
		{
			var filler:Filler = sampleFiller.filler;
			var layer:uint = sampleFiller.layer;
			var idx:uint = layer>0 ? layer-1 : 0; 
			if(view.main.getElementAt(idx) == null) return;
			
			var control:VectorImageFillerDisplay = view.main.getElementAt(idx) as VectorImageFillerDisplay;
			control.setFiller(filler);
			setSampleFillers(sampleFiller);
		}
		
		private function setSampleFillers(item:SampleFiller):void
		{
			_sampleFillers ||= new Array();
			var found:Boolean = false;
			for(var i:uint = 0; i<_sampleFillers.length; i++)
			{
				if(_sampleFillers[i].layer == item.layer)
				{
					_sampleFillers[i] = item;
					found = true;
				}
			}
			if(!found)
			{
				_sampleFillers.push(item);
			}
		}
	}
}